<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <script src="./layui/layui.js"></script>
    <script src="./common copy.js"></script>
</head>

<body>
    <ul class="layui-nav">
        <li class="layui-nav-item userInfo">XXX欢迎登录</li>
    </ul>
    <div class="main">
        <div class="layui-panel">
            <div style="padding: 50px 30px;">

                <table class="layui-table">
                    <thead>
                        <tr>
                            <th>标题</th>
                            <th>发布时间</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- <tr>
                            <td>易烊千玺发表声明</td>
                            <td>2022-07-17</td>
                        </tr> -->
                    </tbody>
                </table>

            </div>
        </div>
    </div>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend></legend>
    </fieldset>
    <div class="demo0"></div>
</body>

<script>
    // 获取用户登录信息  传递的名称需要与设置的名称一致
    // 用户名字userInfo
    let userInfo = getCookie('userInfo') ? JSON.parse(getCookie('userInfo')) : null;

    if (userInfo) {
        // 登录状态
        document.querySelector('.userInfo').innerHTML = `欢迎用户${userInfo.mobile} `
    } else {
        // 未登录状态
        document.querySelector('.userInfo').innerHTML = '<a href="login.html">登录</a>'
    }
    // console.log(userInfo);

    // let tbody = document.querySelector('tbody')

    // 获取新闻列表数据并且渲染

    let xhr = new XMLHttpRequest;
    xhr.open("GET", "http://phpclub.org.cn/edu/server/news.php");
    // 设置请求头
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.send();
    xhr.onload = () => {
        let response = JSON.parse(xhr.responseText);
        // console.log(response);
        response.data.forEach(item => {
            console.log(item)

            //处理获取的时间戳created_at
            var Timestamp = new Date(item.created_at);
            commentTime = Timestamp.toLocaleString();//转为本地时间
            console.log(commentTime);
            // 声明html为空
            let html = '';
            html += `                
                    <tbody>
                        <tr>
                            <td>${item.title}</td>
                            <td>${commentTime}</td>
                        </tr>
                    </tbody>
                `
            document.querySelector('tbody').innerHTML += html;
        });
        // tbody添加事件监听
        let tbody=document.querySelector("tbody");
        tbody.addEventListener('click', e => {
                if (e.target.nodeName === 'TD') {
                    if (userInfo){
                        let trList = document.querySelectorAll('tbody tr')
                        let index = [...trList].indexOf(e.target.parentElement)
                        // removeCookie('newData')
                        setCookie('newId', JSON.stringify(response.data[index].id))
                        console.log(getCookie("newId"));
                        location.href = 'newContent.html'
                    } 
                    else {
                        location.href = 'login.html'
                    }
                }
            })
    }
  
</script>

</html>